package com.itheima.dianming.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.dianming.aop.HasAuth;
import com.itheima.dianming.pojo.domain.*;
import com.itheima.dianming.pojo.vo.R;
import com.itheima.dianming.service.ModelInterfaceService;
import com.itheima.dianming.service.ProjectService;
import com.itheima.dianming.service.StuInterfaceService;
import com.itheima.dianming.service.StuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/module")
@Slf4j
public class ModelInterfaceController {

    @Autowired
    private ModelInterfaceService modelInterfaceService;

    @GetMapping
    public R queryPage(Integer page,Integer pageSize,String name,Long projectId){
        IPage<ModelInterface> pageInfo = new Page<>(page,pageSize);
        LambdaQueryWrapper<ModelInterface> qw = new LambdaQueryWrapper<>();
        if(!StrUtil.isBlank(name)&&!"undefined".equals(name)){
            qw.like(ModelInterface::getInterfaceName,name);
        }
        qw.eq(projectId!=null,ModelInterface::getProjectId,projectId);

        modelInterfaceService.page(pageInfo,qw);
        Map map = new HashMap();
        map.put("records",pageInfo.getRecords());
        map.put("total",pageInfo.getTotal());
        return R.data(map);
    }

    @PostMapping
    @HasAuth("ROLE_ADMIN")
    public R save(@RequestBody ModelInterface modelInterface){
        boolean save = modelInterfaceService.save(modelInterface);
        return R.onflag(save,save?"":"操作失败！");
    }

    @PutMapping
    @HasAuth("ROLE_ADMIN")
    public R update(@RequestBody ModelInterface modelInterface){
        boolean save = modelInterfaceService.updateById(modelInterface);
        return R.onflag(save,save?"":"操作失败！");
    }

    @DeleteMapping("/{id}")
    @HasAuth("ROLE_ADMIN")
    public R deleteById(@PathVariable Integer id){
        boolean b = modelInterfaceService.removeById(id);
        return R.onflag(b);
    }


    @Autowired
    private StuService stuService;

    @Autowired
    private StuInterfaceService stuInterfaceService;

    /**
     * 初始化学生对应接口数据
     * @param projectId 项目id
     * @return
     */
    @PostMapping("/load")
    @HasAuth("ROLE_ADMIN")
    public R loadData(Long projectId){
        List<Stu> list = stuService.list();
        LambdaQueryWrapper<StuInterface> qw = new LambdaQueryWrapper<>();
        qw.eq(StuInterface::getProjectId,projectId);
        stuInterfaceService.remove(qw);
        log.info("清除项目数据{}",projectId);

        LambdaQueryWrapper<ModelInterface> qw1 = new LambdaQueryWrapper<>();
        qw1.orderByAsc(ModelInterface::getId);
        List<ModelInterface> interfaceList = modelInterfaceService.list(qw1);
        for (Stu stu : list) {
            for (ModelInterface modelInterface : interfaceList) {
                StuInterface stuInterface = new StuInterface();
                stuInterface.setProjectId(projectId);//项目id
                stuInterface.setInterfaceId(modelInterface.getId());//接口id
                stuInterface.setStuId(stu.getId());//学生id
                stuInterfaceService.save(stuInterface);
            }
        }
        return R.onflag(true,"加载数据成功");
    }
}
